package network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedList;


/**
 * The class for setting up and controlling the connection to the TimeEdit server. 
 * @author Christian
 *
 */
public class TENetwork {
	private boolean connected;
	public TENetwork(){

	}


	/**
	 * This method takes an URL as parameter and returns a list containing the raw html data from specified URL.
	 * 
	 * @return ArrayList<String> containing the data from the specified URL
	 * @param String src - Source to get data from.
	 * @throws IOException - For some reason unable to read the stream.
	 * @author Mattias
	 */
	public LinkedList<String> getRawDataFromServer(String src) throws IOException{

		//src= "http://timeedit.ita.chalmers.se/4DACTION/WebShowSearch/1/1-0?wv_startWeek=1003&wv_stopWeek=1050&wv_first=0&wv_obj1=5507000&wv_text=Textformat";
		
		
		String sourceURL=src;	
		
		String inputLine;
		InputStream is;
		InputStreamReader data;
		BufferedReader reader;
		URL source;
		LinkedList<String> code;



		source = new URL(sourceURL); 
		is = source.openStream();
		data = new InputStreamReader(is);
		reader = new BufferedReader(data, 8);
		code = new LinkedList<String>();
		
		do{
			try {
				Thread.sleep(10); //For some reason it will not consistently work without a small delay.
			} catch (InterruptedException e) {
				;
			}
		}while(!reader.ready());
		
		if(reader.ready()){
			while((inputLine = reader.readLine()) !=null){
				code.add(inputLine);
			}
			reader.close();
		}
		return code;
	}
	
	
	/** 
	 * Takes a searchquery and returns an URL that leads to a TimeEdit webpage displaying the searchresults.
	 * @param searchQuery 
	 * @return An URL in stringformat
	 * @author Simon
	 */
	public static String createSearchURL(String[] searchQuery){
		String mode = searchQuery[1];
		String query = searchQuery[0];
		int searchType = 0;
		
		if(mode.equals("Class")){
			searchType = 11;
		} else if (mode.equals("Course")) {
			searchType = 3;
		} else if (mode.equals("Classroom")) {
			searchType = 7;
		} else if (mode.equals("Teacher")){
			searchType = 8;
		}
		
		return "http://timeedit.ita.chalmers.se/4DACTION/WebShowSearch/1/1-0?wv_type=" + searchType +"&wv_search=" + query + "&wv_bSearch=S%F6k&wv_startWeek=1003&wv_stopWeek=1050";
	} //http://timeedit.ita.chalmers.se/4DACTION/WebShowSearch/1/1-0?wv_startWeek=1001&wv_stopWeek=1050&wv_first=0&wv_obj1=5507000&wv_text=Textformat
	
	/** 
	 * Takes an integer and returns an URL that leads to a TimeEdit webpage displaying the schedule.
	 * @param query 
	 * @return An URL in stringformat
	 * @author Simon
	 */
	public static String createScheduleURL(int query){
		
		return "http://timeedit.ita.chalmers.se/4DACTION/WebShowSearch/1/1-0?wv_startWeek=1003&wv_stopWeek=1005&wv_first=0&wv_obj1=" + query + "&wv_text=Textformat";
	}
	

	private void submitSearch(){

	}


	private void retriveSearch()
	{

	}


	private boolean connectToTE() {
		return false;
	}

	public void setConnected(boolean connected) {
		this.connected = connected;
	}

	public boolean isConnected() {
		return connected;
	}

}
